package com.base.monitor.framework.result;

import com.base.monitor.model.enums.code.BaseCode;
import com.base.monitor.model.enums.code.impl.common.SuccessCode;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 通用结果返回类
 */
@Data
@NoArgsConstructor
public class Result<T> {
    private boolean success;
    private int code;
    private String message;
    private T data;

    public Result(BaseCode code) {
        this(code, code.message());
    }

    public Result(BaseCode code, String msg) {
        this(code, msg, null);
    }

    public Result(BaseCode code, String msg, T data) {
        this.success = code.success();
        this.code = code.code();
        this.message = msg;
        this.data = data;
    }

    public static Result<Void> success() {
        return new Result<>(SuccessCode.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        Result<T> commonResult = new Result<>(SuccessCode.SUCCESS);
        commonResult.setData(data);
        return commonResult;
    }

    public static Result<Void> failure(BaseCode code) {
        return new Result(code);
    }

    public static Result<Void> failure(BaseCode code, String message) {
        return new Result<>(code, message);
    }
}